package gang.org.springframework.boot;

import gang.org.springframework.framework.context.GangConfigurableApplicationContext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author gang.chen
 * @description
 * @Date 2021/6/11 13:34
 */
public class GangSpringApplicationRunListeners {

    private final Object log;

    /**
     * 接口GangSpringApplicationRunListener ->实现类GangEventPublishingRunListener
     * */
    private final List<GangSpringApplicationRunListener> listeners;

    private final Object applicationStartup;

    public GangSpringApplicationRunListeners(Object log, Collection<GangSpringApplicationRunListener> listeners, Object applicationStartup) {
        this.log = log;
        this.listeners = new ArrayList<>(listeners);
        this.applicationStartup = applicationStartup;
    }

    /**
     * TODO
     * 业务意义
     * ?
     * */
    void starting(Object bootstrapContext, Class mainApplicationClass){

    }

    void started(GangConfigurableApplicationContext context){

    }

    void running(GangConfigurableApplicationContext context){

    }

    void contextLoaded(GangConfigurableApplicationContext context){
        doWithListeners("",(listener) -> listener.contextLoaded(context));
    }

    /**
     * @param listenerAction lambda表达式，可以理解为一个运行方法
     * */
    private void doWithListeners(String stepName, Consumer<GangSpringApplicationRunListener> listenerAction){
        doWithListeners(stepName, listenerAction,null);
    }

    private void doWithListeners(String stepName, Consumer<GangSpringApplicationRunListener> listenerAction, Object obj){
        this.listeners.forEach(listenerAction);
    }

}
